package com.me.forum.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.me.enums.RespType;
import com.me.forum.domain.Category;
import com.me.forum.request.CategoryRequest;
import com.me.forum.service.impl.CategoryService;
import com.me.forum.utils.components.model.IModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class AdminCategoryController {
    //by lq
    @Autowired
    private CategoryService categoryService;


    //通过id查询分区
    @GetMapping("/{id}")
    public IModel getById(@PathVariable String id, IModel model){
        Category category = categoryService.findById(id);
        model.put("success", category != null);
        model.put("category", category);
        return model;
    }

    //获取所有分区、
    @GetMapping
    public IModel getAllCategory(@RequestBody CategoryRequest creq, IModel model,String page,String size){
        IPage<Category> categorys = categoryService.getCategory(creq,page,size);
        model.success(categorys!= null && categorys.getSize() != 0);
        model.add(RespType.Categories,categorys);
        return model;
    }

    //添加分区
    @PostMapping
    public IModel CategoryAdd(@RequestBody CategoryRequest creq, IModel model) {
        Category category = categoryService.addCategory(creq);
        model.success(category != null);
        return model;
    }

    //修改分区名字数据
    @PutMapping
    public IModel CategoryUpdate(@RequestBody CategoryRequest creq,IModel model) {
        model.success(categoryService.updateByName(creq));
        return model;
    }

    //删除分区
    @DeleteMapping
    private IModel CategoryDelete(@PathVariable String id,IModel model) {
        model.success(categoryService.delete(id));
        return model;
    }
}
